package com.dc.ftp.utils;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPopupWindow;
import com.bigkoo.pickerview.TimePopupWindow;
import com.dc.ftp.R;
import com.dc.ftp.view.address_popwindow.PopHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2018/1/13.
 */

public class PickerViewUtil {


    public static void initPickerView(Context context, View parentView,boolean recycle, String title, final ArrayList<String> mList, final TextView tv){

        //module中的第三方pickerView---支持一二三级联动，界面风格改成橙色
        OptionsPopupWindow   mPop = new OptionsPopupWindow(context);
        mPop.setTitle(title);
        mPop.setPicker(mList);
        mPop.setCyclic(recycle);//item是否循环显示
        mPop.showAtLocation(parentView, Gravity.BOTTOM,0,0);
        mPop.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                tv.setText(mList.get(options1));
            }
        });


    }

    public static void initTimePickerView(Context context, View parentView,boolean recycle,final TextView tv){
        TimePopupWindow mTimePop = new TimePopupWindow(context, TimePopupWindow.Type.YEAR_MONTH_DAY);
        mTimePop.setCyclic(recycle);
        mTimePop.showAtLocation(parentView,Gravity.BOTTOM,0,0);
        mTimePop.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                tv.setText( format.format(date).toString());
            }
        });
    }


    //另一款三级联动地址选择PickerView
    public static void showAddressPickerView(Context context,View parentView,final TextView tv){

        //若软键盘打开则关闭，否则popwindow会被顶上去
        SystemApi.hiddenKeyBoard((Activity)context);

        final PopHelper popHelper = new PopHelper(context);
        popHelper.showAddressPop(parentView, (Activity)context, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_pop_address_cencel:
                        popHelper.colsePopupwindow();
                        break;
                    case R.id.btn_pop_address_enter:
                        String addressData = popHelper.getAddressData();
                        String begain,end="";
                        if(addressData.startsWith("北京市")||addressData.startsWith("上海市")||addressData.startsWith("天津市")||addressData.startsWith("重庆市")){
                            begain = addressData.substring(0,3);
                            String s = addressData.substring(3);
                            if(s.startsWith("市辖区")){
                                end = s.substring(3);
                            }else if(s.startsWith("市")||s.startsWith("县")){
                                end = s.substring(1);
                            }
                            addressData = begain + end;
                        }
                        tv.setText(addressData);
                        popHelper.colsePopupwindow();
                        break;
                }
            }
        });
    }




}